<?php
if(!defined('IN_SITE')) {
	exit('Access Denied');
}

class system_setting{
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();

	static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}

	/*
	*  更新系统设置
	*/
	public function system_info($info) {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$info = isset($_REQUEST['info']) && !empty($_REQUEST['info']) ? $_REQUEST['info']:'';
		if('1'==$issubmit && !empty($info)){
			if (!empty($info['sitedomain'])) {
				@DB::insert('common_setting', array('skey' => 'sitedomain', 'svalue' => $info['sitedomain']), false, true);
			}
			if (!empty($info['sitename'])) {
				@DB::insert('common_setting', array('skey' => 'sitename', 'svalue' => $info['sitename']), false, true);
			}
			if (!empty($info['system_logo'])) {
				@DB::insert('common_setting', array('skey' => 'system_logo', 'svalue' => $info['system_logo']), false, true);
			}
			if (!empty($info['system_logo_footer'])) {
				@DB::insert('common_setting', array('skey' => 'system_logo_footer', 'svalue' => $info['system_logo_footer']), false, true);
			}
			if (!empty($info['seotitle'])) {
				@DB::insert('common_setting', array('skey' => 'seotitle', 'svalue' => $info['seotitle']), false, true);
			}
			if (!empty($info['seokeywords'])) {
				@DB::insert('common_setting', array('skey' => 'seokeywords', 'svalue' => $info['seokeywords']), false, true);
			}
			if (!empty($info['seodescription'])) {
				@DB::insert('common_setting', array('skey' => 'seodescription', 'svalue' => $info['seodescription']), false, true);
			}
			if (!empty($info['site_icp'])) {
				@DB::insert('common_setting', array('skey' => 'site_icp', 'svalue' => $info['site_icp']), false, true);
			}
			if (!empty($info['system_info1'])) {
				@DB::insert('common_setting', array('skey' => 'system_info1', 'svalue' => $info['system_info1']), false, true);
			}
			if (!empty($info['system_info2'])) {
				@DB::insert('common_setting', array('skey' => 'system_info2', 'svalue' => $info['system_info2']), false, true);
			}
			if (!empty($info['system_info3'])) {
				@DB::insert('common_setting', array('skey' => 'system_info3', 'svalue' => $info['system_info3']), false, true);
			}
			if (!empty($info['system_qq_customer'])) {
				@DB::insert('common_setting', array('skey' => 'system_qq_customer', 'svalue' => $info['system_qq_customer']), false, true);
			}
			if (!empty($info['system_service_time'])) {
				@DB::insert('common_setting', array('skey' => 'system_service_time', 'svalue' => $info['system_service_time']), false, true);
			}
			if (!empty($info['system_service_phone'])) {
				@DB::insert('common_setting', array('skey' => 'system_service_phone', 'svalue' => $info['system_service_phone']), false, true);
			}
            if (!empty($info['system_weixin_qr'])) {
				@DB::insert('common_setting', array('skey' => 'system_weixin_qr', 'svalue' => $info['system_weixin_qr']), false, true);
			}
            if (!empty($info['system_service_email'])) {
				@DB::insert('common_setting', array('skey' => 'system_service_email', 'svalue' => $info['system_service_email']), false, true);
			}
            if (!empty($info['system_friend_link'])) {
				@DB::insert('common_setting', array('skey' => 'system_friend_link', 'svalue' => $info['system_friend_link']), false, true);
			}
            if (!empty($info['system_sms_api'])) {
				@DB::insert('common_setting', array('skey' => 'system_sms_api', 'svalue' => $info['system_sms_api']), false, true);
			}
            if (!empty($info['system_sms_account'])) {
				@DB::insert('common_setting', array('skey' => 'system_sms_account', 'svalue' => $info['system_sms_account']), false, true);
			}
            if (!empty($info['system_sms_content'])) {
				@DB::insert('common_setting', array('skey' => 'system_sms_content', 'svalue' => $info['system_sms_content']), false, true);
			}
            
            if (!empty($info['system_offline_bank_name'])) {
				@DB::insert('common_setting', array('skey' => 'system_offline_bank_name', 'svalue' => $info['system_offline_bank_name']), false, true);
			}
            if (!empty($info['system_offline_bank_account'])) {
				@DB::insert('common_setting', array('skey' => 'system_offline_bank_account', 'svalue' => $info['system_offline_bank_account']), false, true);
			}
            if (!empty($info['system_offline_bank_company'])) {
				@DB::insert('common_setting', array('skey' => 'system_offline_bank_company', 'svalue' => $info['system_offline_bank_company']), false, true);
			}
            if (!empty($info['system_school_type'])) {
				@DB::insert('common_setting', array('skey' => 'system_school_type', 'svalue' => $info['system_school_type']), false, true);
			}
            if (!empty($info['service_terms'])) {
				@DB::insert('common_setting', array('skey' => 'service_terms', 'svalue' => $info['service_terms']), false, true);
			}
            //DEBUG 看回放设置
            if (!empty($info['system_course_vod_view_num'])) {
				@DB::insert('common_setting', array('skey' => 'system_course_vod_view_num', 'svalue' => $info['system_course_vod_view_num']), false, true);
			}
            if (!empty($info['system_course_vod_view_days'])) {
				@DB::insert('common_setting', array('skey' => 'system_course_vod_view_days', 'svalue' => $info['system_course_vod_view_days']), false, true);
			}
            if (!empty($info['system_course_vod_view_starttime'])) {
				@DB::insert('common_setting', array('skey' => 'system_course_vod_view_starttime', 'svalue' => $info['system_course_vod_view_starttime']), false, true);
			}
            if (!empty($info['system_course_vod_view_endtime'])) {
				@DB::insert('common_setting', array('skey' => 'system_course_vod_view_endtime', 'svalue' => $info['system_course_vod_view_endtime']), false, true);
			}
            if (!empty($info['system_pub_type'])) {
				@DB::insert('common_setting', array('skey' => 'system_pub_type', 'svalue' => $info['system_pub_type']), false, true);
			}
            if (!empty($info['system_course_cate'])) {
				@DB::insert('common_setting', array('skey' => 'system_course_cate', 'svalue' => $info['system_course_cate']), false, true);
			}
            //DEBUG 支付宝接口参数设置 开始
            if (!empty($info['system_alipay_partner'])) {
				@DB::insert('common_setting', array('skey' => 'system_alipay_partner', 'svalue' => $info['system_alipay_partner']), false, true);
			}
            if (!empty($info['system_alipay_seller_email'])) {
				@DB::insert('common_setting', array('skey' => 'system_alipay_seller_email', 'svalue' => $info['system_alipay_seller_email']), false, true);
			}
            if (!empty($info['system_alipay_key'])) {
				@DB::insert('common_setting', array('skey' => 'system_alipay_key', 'svalue' => $info['system_alipay_key']), false, true);
			}
            if (!empty($info['system_shopping_value'])) {
				@DB::insert('common_setting', array('skey' => 'system_shopping_value', 'svalue' => $info['system_shopping_value']), false, true);
			}
            if (!empty($info['system_express_price'])) {
				@DB::insert('common_setting', array('skey' => 'system_express_price', 'svalue' => $info['system_express_price']), false, true);
			}
            //DEBUG 支付宝接口参数设置 结束
            $this->return_json(false,'操作成功');
		}else{
			@updatecache(array('setting'));
            //DEBUG 处理标签冲突
            $php_tag = '$';
            //DEBUG 生成学段缓存文件
            $cache_school_type_json_string = '';
            $system_school_type = $tmp1 = $tmp2 = array();
            if($_G['setting']['system_school_type']){
                $tmp1 = explode(',',$_G['setting']['system_school_type']);
                foreach($tmp1 AS $key => $value){
                    $tmp2 = explode('|',$value);
                    $system_school_type[$tmp2[0]] = $tmp2[1];
                }
            }
            foreach($system_school_type AS $key => $value){
                $cache_school_type_json_string .= '{"n": "'.$value.'", "v": "'.$key.'"},';
            }
            $cache_school_type_json_string = '['.trim($cache_school_type_json_string,',').']';
            //$cache_school_type_json_string = diconv( $cache_school_type_json_string, 'UTF-8','GB2312');
            @file_put_contents(SITE_ROOT.'./data/sysdata/grade.json',$cache_school_type_json_string);
            //DEBUG 生成入学年份缓存文件
            $cache_year_json_string = '[{"n": "'.date('Y').'", "v": "'.date('Y').'"},{"n": "'.(date('Y')-1).'", "v": "'.(date('Y')-1).'"},{"n": "'.(date('Y')-2).'", "v": "'.(date('Y')-2).'"},{"n": "'.(date('Y')-3).'", "v": "'.(date('Y')-3).'"}]';
            $cache_year_json_string = diconv( $cache_year_json_string, 'UTF-8','GB2312');
            @file_put_contents(SITE_ROOT.'./data/sysdata/year.json',$cache_year_json_string);
			include template('admin/system/system_setting');	
		}
	}

	/*
	* ajax 返回信息
	*/
	public function return_json($error,$message,$output=1) {
		$return_array = array(
			"error" => $error,
			"message" => $message
		);
		if($output){
			echo json_ext($return_array);
			die;
		}else{
			return json_ext($return_array);
		}
	}
}
?>